package com.sz.biz.common.project.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.project.entity.ProjectAudit;
import com.sz.biz.common.project.entity.ProjectQuestion;
import com.sz.biz.common.project.service.ProjectAuditService;
import com.sz.biz.common.project.service.ProjectQuestionService;
import com.sz.biz.common.project.dto.ProjectAuditInfoDto;
import com.sz.biz.common.project.dto.ProjectFileDto;
import com.sz.biz.common.project.dto.ProjectQuestionDto;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.system.entity.SysFile;
import com.sz.common.core.system.service.SysFileService;
import com.sz.common.core.system.dto.UserDto;
import com.sz.common.core.system.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * Function: TODO: ADD FUNCTION <br>
 * Author: qiankun<br>
 * Date: 2017-07-13 08:55:00
 */
@Service("ProjectAuditService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProjectAuditServiceImpl extends AbstractService implements ProjectAuditService {

    @Autowired
    private ProjectQuestionService projectQuestionService;

    @Autowired
    private UserService userService;

    @Autowired
    private SysFileService sysFileService;

    @Override
    public int save(ProjectAudit entry) {
        checkDataValid(entry);
        dao.save("ProjectAuditMapper.insert", entry);
        queryByProjectId(entry.getProjectId());
        return entry.getId();
    }

    private void checkDataValid(ProjectAudit entry) {
        if (entry != null) {
            if (StringUtils.isEmpty(entry.getProjectId()) || entry.getProjectId() == 0) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
            }
            if (StringUtils.isEmpty(entry.getAuditorId()) || entry.getAuditorId() == 0) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "auditorId");
            }
            if (StringUtils.isEmpty(entry.getType()) || entry.getType() < 0 || entry.getType() > 4) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "type");
            }
            if (entry.getIsPass() == null) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "isPass");
            }
            if (StringUtils.isEmpty(entry.getMsg() ) ) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "msg");
            }
        }
    }

    @Override
    public int update(ProjectAudit entry) {
        if (entry.getId() == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "id");
        }
        checkDataValid(entry);
        dao.update("ProjectAuditMapper.update", entry);
        return entry.getId();
    }

    @Override
    public List<ProjectAudit> queryByProjectId(int projectId) {
        if (projectId == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "id");
        }
        List<ProjectAudit> listResult = dao.findForList("ProjectAuditMapper.queryByProject", projectId, ProjectAudit
                .class);
        return listResult;
    }

    @Override
    public ProjectAuditInfoDto getProjectAuditInfoDtoByProjectIdAndType(int projectId, int type) {
        ProjectAuditInfoDto projectAuditInfoDto = new ProjectAuditInfoDto();
        if (projectId == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
        }
        if (type == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "type");
        }
        Map<String, Object> pd = new HashMap<>();
        pd.put("projectId", projectId);
        pd.put("type", type);

        List<ProjectAudit> projectAuditList = dao.findForList("ProjectAuditMapper.queryByProjectAndType", pd,
                ProjectAudit.class);

        if (projectAuditList.size() > 0) {
            ProjectAudit projectAudit = projectAuditList.get(0);
            BeanUtils.copyProperties(projectAudit, projectAuditInfoDto);
            if (!StringUtils.isEmpty(projectAuditInfoDto.getAuditorId()) && projectAuditInfoDto.getAuditorId() != 0) {
                UserDto userDto = userService.getUserById(projectAuditInfoDto.getAuditorId());
                if (userDto != null) {
                    projectAuditInfoDto.setAuditorName(userDto.getFullName());
                }
            }
        }
        List<ProjectQuestion> projectQuestionList = projectQuestionService.queryByProjectIdAndType(projectId, type);
        if (!CollectionUtils.isEmpty(projectQuestionList)) {
            projectQuestionList.forEach(projectQuestion -> {
                ProjectQuestionDto projectQuestionDto = new ProjectQuestionDto();
                BeanUtils.copyProperties(projectQuestion, projectQuestionDto);

                UserDto askUser = userService.getUserById(projectQuestion.getUserId());
                if (askUser != null) {
                    projectQuestionDto.setUserName(askUser.getFullName());
                }

                List<String> fileIds = getListFiles(projectQuestionDto.getFileIds());
                if (fileIds != null && fileIds.size() > 0) {
                    List<SysFile> fileInfos = sysFileService.getFileInfos(fileIds);
                    for (SysFile file : fileInfos) {
                        ProjectFileDto projectFileDto = new ProjectFileDto();
                        projectFileDto.setFileName(file.getFileName());
                        projectFileDto.setFilePath(file.getFileLocation());
                        projectFileDto.setContentType(file.getContentType());
                        projectFileDto.setFileId(file.getFileId());
                        projectQuestionDto.getProjectFileDtos().add(projectFileDto);
                    }
                }
                projectAuditInfoDto.getProjectQuestionDtoList().add(projectQuestionDto);
            });
        }
        return projectAuditInfoDto;
    }

    private List<String> getListFiles(String fileId) {
        List<String> list = new ArrayList<>();
        if (!StringUtils.isEmpty(fileId)) {
            String[] files = fileId.split(",");
            list = Arrays.asList(files);
        }
        return list;
    }

    @Override
    public void deleteByProjectId(int projectId) {
        dao.delete("ProjectAuditMapper.deleteByProjectId", projectId);
    }
}
